package copyengine.ui.component.button.animation.pc
{
	import copyengine.ui.component.list.old.animation.ICEListCellRenderAnimation;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import copyengine.ui.component.button.animation.ICEButtonAnimation;

	public class CEButtonFrameAnimation implements ICEButtonAnimation
	{
		private static const FRAME_ON:String = "on";
		private static const FRAME_DOWN:String = "down";
		private static const FRAME_OVER:String = "over";

		private static const FRAME_TYPE_ON:int = 0;
		private static const FRAME_TYPE_DOWN:int = 1;
		private static const FRAME_TYPE_OVER:int = 2;

		private var frameOn:String;
		private var frameDown:String;
		private var frameOver:String;

		private var currentFrameType:int;

		protected var targetBg:MovieClip;

		/**
		 * use an property to record current btn frma lable.
		 */
		protected var currentFrameLable:String;

		public function CEButtonFrameAnimation(_args:Object = null)
		{
		}

		public function setTarget(_buttonBg:DisplayObject):void
		{
			targetBg = _buttonBg as MovieClip;
			//如果按钮含有bgMc属性则表示按钮为Label+Bg模式 此时只操作bgMc
			if (targetBg["bgMc"] != null)
			{
				targetBg = targetBg["bgMc"];
			}

			changeBtnFrameType(FRAME_ON, FRAME_DOWN, FRAME_OVER);
			changeToFrame(FRAME_ON);
		}

		public function dispose():void
		{
			targetBg.stop();
			targetBg = null;
		}

		public function onMouseUp(e:MouseEvent):void
		{
			currentFrameType = FRAME_TYPE_OVER;
			changeToFrame(frameOver);
		}

		public function onMouseDown(e:MouseEvent):void
		{
			currentFrameType = FRAME_TYPE_DOWN;
			changeToFrame(frameDown);
		}

		public function onMouseRollOver(e:MouseEvent):void
		{
			currentFrameType = FRAME_TYPE_OVER;
			changeToFrame(frameOver);
		}

		public function onMouseRollOut(e:MouseEvent):void
		{
			currentFrameType = FRAME_TYPE_ON;
			changeToFrame(frameOn);
		}

		protected final function changeToFrame(_frameLable:String):void
		{
			if (currentFrameLable != _frameLable)
			{
				currentFrameLable = _frameLable;
				refreshBtn();
			}
		}

		protected final function changeBtnFrameType(_frameOn:String, _frameDown:String, _frameOver:String):void
		{
			frameOn = _frameOn;
			frameDown = _frameDown;
			frameOver = _frameOver;
			refreshBtnByCurrentType();
		}

		protected final function refreshBtn():void
		{
			targetBg.gotoAndStop(currentFrameLable);
		}

		private function refreshBtnByCurrentType():void
		{
			switch (currentFrameType)
			{
				case FRAME_TYPE_ON:
					changeToFrame(frameOn);
					break;
				case FRAME_TYPE_DOWN:
					changeToFrame(frameDown);
					break;
				case FRAME_TYPE_OVER:
					changeToFrame(frameOver);
					break;
			}
		}

	}
}
